package com.jianda.view;

import com.jianda.commons.content.Content;
import com.jianda.entity.dto.WaveDto;
import com.jianda.service.WaveDataService;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.List;

import static com.jianda.entity.dto.WaveDto.ay1;
import static com.jianda.entity.dto.WaveDto.ay2;
import static com.jianda.entity.dto.WaveDto.by1;
import static com.jianda.entity.dto.WaveDto.by2;
import static com.jianda.entity.dto.WaveDto.cy1;
import static com.jianda.entity.dto.WaveDto.cy2;
import static com.jianda.entity.dto.WaveDto.dy1;
import static com.jianda.entity.dto.WaveDto.dy2;
import static com.jianda.entity.dto.WaveDto.ey1;
import static com.jianda.entity.dto.WaveDto.ey2;
import static com.jianda.entity.dto.WaveDto.fy1;
import static com.jianda.entity.dto.WaveDto.fy2;
import static com.jianda.entity.dto.WaveDto.gy1;
import static com.jianda.entity.dto.WaveDto.gy2;
import static com.jianda.entity.dto.WaveDto.hy1;
import static com.jianda.entity.dto.WaveDto.hy2;
import static com.jianda.entity.dto.WaveDto.iy1;
import static com.jianda.entity.dto.WaveDto.iy2;
import static com.jianda.entity.dto.WaveDto.jy1;
import static com.jianda.entity.dto.WaveDto.jy2;
import static com.jianda.service.WaveDataService.MAX_COUNT_OF_VALUES;
import static com.jianda.service.WaveDataService.values;
import static com.jianda.commons.content.Content.intervalWave;
import static com.jianda.commons.content.Content.intervalWaveGap;

/**
 * 波形数据展示类
 */
public class WaveView {
    protected static int x1;
    protected static int x2;
    protected static int thish;

    public static void waveView1(int w, int h,
                                 Graphics2D g2d, List<Integer> values,
                                 JFrame jFrame) {
        int xDelta = w / MAX_COUNT_OF_VALUES;
        int length = values.size();
        thish = h;
        Ellipse2D.Double ellipse = new Ellipse2D.Double(jFrame.getWidth() * 0.015, jFrame.getHeight() * 0.020,
                jFrame.getWidth(), jFrame.getHeight());
       // System.out.println("jFrame.getWidth():" + jFrame.getWidth());
       // System.out.println("jFrame.getHeight():" + jFrame.getHeight());

        // 坐标值
        x1 = 0;
        x2 = 0;

        // 设置宽度和间隔
        intervalWave = jFrame.getHeight() * 0.017;
        intervalWaveGap =  jFrame.getHeight() * 0.075;
        //System.out.println("interalWave:" + intervalWave);
        //System.out.println("intervalWaveGap:" + intervalWaveGap);

        for (int i = 0; i < values.size() - 1; ++i) {
            // a波形
            waveA(i, xDelta, length, values, g2d);
            waveB(i, xDelta, length, values, g2d);
            waveC(i, xDelta, length, values, g2d);
            waveD(i, xDelta, length, values, g2d);
            waveE(i, xDelta, length, values, g2d);
            waveF(i, xDelta, length, values, g2d);
            waveG(i, xDelta, length, values, g2d);
            waveH(i, xDelta, length, values, g2d);
            waveI(i, xDelta, length, values, g2d);
            waveJ(i, xDelta, length, values, g2d);
        }
    }

    // a波形
    private static void waveA(int i, int xDelta, int length, List<Integer> values, Graphics2D g2d) {
        x1 = xDelta * (MAX_COUNT_OF_VALUES - length + i);
        ay1 = normalizeValueForYAxis(values.get(i), thish);
        x2 = xDelta * (MAX_COUNT_OF_VALUES - length + i + 1);
        ay2 = normalizeValueForYAxis(values.get(i + 1), thish);
        g2d.setColor(Content.ColorContent.color1);
        g2d.drawLine(x1, ay1, x2, ay2);
    }

    // b波形
    private static void waveB(int i, int xDelta, int length, List<Integer> values, Graphics2D g2d) {
        x1 = xDelta * (MAX_COUNT_OF_VALUES - length + i);
        by1 =  (int)(normalizeValueForYAxis(values.get(i), thish) + intervalWave + intervalWaveGap);
        x2 = xDelta * (MAX_COUNT_OF_VALUES - length + i + 1);
        by2 =  (int)(normalizeValueForYAxis(values.get(i + 1), thish) + intervalWave + intervalWaveGap);
        g2d.setColor(Content.ColorContent.color2);
        g2d.drawLine(x1, by1, x2, by2);
    }

    // c波形
    private static void waveC(int i, int xDelta, int length, List<Integer> values, Graphics2D g2d) {
        x1 = xDelta * (MAX_COUNT_OF_VALUES - length + i);
        cy1 =  (int)(normalizeValueForYAxis(values.get(i), thish) + (intervalWave * 2) + (intervalWaveGap * 2));
        x2 = xDelta * (MAX_COUNT_OF_VALUES - length + i + 1);
        cy2 =  (int)(normalizeValueForYAxis(values.get(i + 1), thish) + (intervalWave * 2) + (intervalWaveGap * 2));
        g2d.setColor(Content.ColorContent.color3);
        g2d.drawLine(x1, cy1, x2, cy2);
    }

    // d波形
    private static void waveD(int i, int xDelta, int length, List<Integer> values, Graphics2D g2d) {
        x1 = xDelta * (MAX_COUNT_OF_VALUES - length + i);
        dy1 =  (int)(normalizeValueForYAxis(values.get(i), thish) + (intervalWave * 3) + (intervalWaveGap * 3));
        x2 = xDelta * (MAX_COUNT_OF_VALUES - length + i + 1);
        dy2 =  (int)(normalizeValueForYAxis(values.get(i + 1), thish) + (intervalWave * 3) + (intervalWaveGap * 3));
        g2d.setColor(Content.ColorContent.color1);
        g2d.drawLine(x1, dy1, x2, dy2);
    }

    // e波形
    private static void waveE(int i, int xDelta, int length, List<Integer> values, Graphics2D g2d) {
        x1 = xDelta * (MAX_COUNT_OF_VALUES - length + i);
        ey1 =  (int)(normalizeValueForYAxis(values.get(i), thish) + (intervalWave * 4) + (intervalWaveGap * 4));
        x2 = xDelta * (MAX_COUNT_OF_VALUES - length + i + 1);
        ey2 =  (int)(normalizeValueForYAxis(values.get(i + 1), thish) + (intervalWave * 4) + (intervalWaveGap * 4));
        g2d.setColor(Content.ColorContent.color2);
        g2d.drawLine(x1, ey1, x2, ey2);
    }

    // f波形
    private static void waveF(int i, int xDelta, int length, List<Integer> values, Graphics2D g2d) {
        x1 = xDelta * (MAX_COUNT_OF_VALUES - length + i);
        fy1 =  (int)(normalizeValueForYAxis(values.get(i), thish) + (intervalWave * 5) + (intervalWaveGap * 5));
        x2 = xDelta * (MAX_COUNT_OF_VALUES - length + i + 1);
        fy2 =  (int)(normalizeValueForYAxis(values.get(i + 1), thish) + (intervalWave * 5) + (intervalWaveGap * 5));
        g2d.setColor(Content.ColorContent.color3);
        g2d.drawLine(x1, fy1, x2, fy2);
    }

    // g波形
    private static void waveG(int i, int xDelta, int length, List<Integer> values, Graphics2D g2d) {
        x1 = xDelta * (MAX_COUNT_OF_VALUES - length + i);
        gy1 =  (int)(normalizeValueForYAxis(values.get(i), thish) + (intervalWave * 6) + (intervalWaveGap * 6));
        x2 = xDelta * (MAX_COUNT_OF_VALUES - length + i + 1);
        gy2 =  (int)(normalizeValueForYAxis(values.get(i + 1), thish) + (intervalWave * 6) + (intervalWaveGap * 6));
        g2d.setColor(Content.ColorContent.color1);
        g2d.drawLine(x1, gy1, x2, gy2);
    }

    // h波形
    private static void waveH(int i, int xDelta, int length, List<Integer> values, Graphics2D g2d) {
        x1 = xDelta * (MAX_COUNT_OF_VALUES - length + i);
        hy1 =  (int)(normalizeValueForYAxis(values.get(i), thish) + (intervalWave * 7) + (intervalWaveGap * 7));
        x2 = xDelta * (MAX_COUNT_OF_VALUES - length + i + 1);
        hy2 =  (int)(normalizeValueForYAxis(values.get(i + 1), thish) + (intervalWave * 7) + (intervalWaveGap * 7));
        g2d.setColor(Content.ColorContent.color2);
        g2d.drawLine(x1, hy1, x2, hy2);
    }

    // i波形
    private static void waveI(int i, int xDelta, int length, List<Integer> values, Graphics2D g2d) {
        x1 = xDelta * (MAX_COUNT_OF_VALUES - length + i);
        iy1 =  (int)(normalizeValueForYAxis(values.get(i), thish) + (intervalWave * 8) + (intervalWaveGap * 8));
        x2 = xDelta * (MAX_COUNT_OF_VALUES - length + i + 1);
        iy2 =  (int)(normalizeValueForYAxis(values.get(i + 1), thish) + (intervalWave * 8) + (intervalWaveGap * 8));
        g2d.setColor(Content.ColorContent.color3);
        g2d.drawLine(x1, iy1, x2, iy2);
    }

    // j波形
    private static void waveJ(int i, int xDelta, int length, List<Integer> values, Graphics2D g2d) {
        x1 = xDelta * (MAX_COUNT_OF_VALUES - length + i);
        jy1 =  (int)(normalizeValueForYAxis(values.get(i), thish) + (intervalWave * 9) + (intervalWaveGap * 9));
        x2 = xDelta * (MAX_COUNT_OF_VALUES - length + i + 1);
        jy2 =  (int)(normalizeValueForYAxis(values.get(i + 1), thish) + (intervalWave * 9) + (intervalWaveGap * 9));
        g2d.setColor(Content.ColorContent.color1);
        g2d.drawLine(x1, jy1, x2, jy2);
    }

    /**
     * 规一化y轴方向的值. 使得value在y轴的值为[0, height]之间.
     *
     * @param value
     * @param height
     * @return
     */
    private static int normalizeValueForYAxis(int value, int height) {
        return (int) ((double) height / WaveDataService.MAX_VALUE * value);
    }
}
